package com.huashi.dealer.modular.controller.store.region;

import com.huashi.dealer.core.pojo.CommonResult;
import com.huashi.dealer.modular.business.region.param.RegionListItemVo;
import com.huashi.dealer.modular.business.region.param.RegionTreeVo;
import com.huashi.dealer.modular.business.region.service.RegionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 地域控制器
 *
 * @author 系统生成
 * @since 2025-01-27
 */
@Slf4j
@Tag(name = "地域控制器")
@RestController("storeRegionController")
@RequestMapping("/store/region")
@Validated
public class RegionController {

    @Resource
    private RegionService regionService;

    /**
     * 获取省份列表
     */
    @GetMapping("/provinces")
    @Operation(summary = "获取省份列表", description = "获取所有省份列表，用于地域选择")
    public CommonResult<List<RegionListItemVo>> getProvinces() {
        try {
            List<RegionListItemVo> provinces = regionService.getProvinces();
            return CommonResult.data(provinces);
        } catch (Exception e) {
            return CommonResult.error("获取省份列表失败：" + e.getMessage());
        }
    }

    /**
     * 获取城市列表
     */
    @GetMapping("/cities")
    @Operation(summary = "获取城市列表", description = "根据省份ID获取对应的城市列表")
    public CommonResult<List<RegionListItemVo>> getCities(
            @Parameter(description = "省份ID") @RequestParam Long provinceId) {
        try {
            List<RegionListItemVo> cities = regionService.getCitiesByProvince(provinceId);
            return CommonResult.data(cities);
        } catch (Exception e) {
            return CommonResult.error("获取城市列表失败：" + e.getMessage());
        }
    }

    /**
     * 获取区县列表
     */
    @GetMapping("/regions")
    @Operation(summary = "获取区县列表", description = "根据城市ID获取对应的区县列表")
    public CommonResult<List<RegionListItemVo>> getRegions(
            @Parameter(description = "城市ID") @RequestParam Long cityId) {
        try {
            List<RegionListItemVo> regions = regionService.getRegionsByCity(cityId);
            return CommonResult.data(regions);
        } catch (Exception e) {
            return CommonResult.error("获取区县列表失败：" + e.getMessage());
        }
    }

    /**
     * 根据父级ID获取地域列表
     */
    @GetMapping("/list")
    @Operation(summary = "根据父级ID获取地域列表", description = "根据父级ID获取地域列表，parentId=0获取省份列表")
    public CommonResult<List<RegionListItemVo>> getRegionList(
            @Parameter(description = "父级ID，0表示获取省份列表") @RequestParam(defaultValue = "0") Long parentId) {
        try {
            List<RegionListItemVo> regions = regionService.getRegionListByParentId(parentId);
            return CommonResult.data(regions);
        } catch (Exception e) {
            return CommonResult.error("获取地域列表失败：" + e.getMessage());
        }
    }

    /**
     * 根据地域ID获取地域名称
     */
    @GetMapping("/name/{regionId}")
    @Operation(summary = "根据地域ID获取地域名称", description = "根据地域ID获取对应的地域名称")
    public CommonResult<String> getRegionName(
            @Parameter(description = "地域ID") @PathVariable Long regionId) {
        try {
            String regionName = regionService.getRegionNameById(regionId);
            return CommonResult.data(regionName);
        } catch (Exception e) {
            return CommonResult.error("获取地域名称失败：" + e.getMessage());
        }
    }

    /**
     * 获取完整的省市区信息
     */
    @GetMapping("/fullName")
    @Operation(summary = "获取完整的省市区信息", description = "根据省份ID、城市ID、区县ID获取完整的省市区信息")
    public CommonResult<String> getFullRegionName(
            @Parameter(description = "省份ID") @RequestParam(required = false) Long provinceId,
            @Parameter(description = "城市ID") @RequestParam(required = false) Long cityId,
            @Parameter(description = "区县ID") @RequestParam(required = false) Long regionId) {
        try {
            String fullRegionName = regionService.getFullRegionName(provinceId, cityId, regionId);
            return CommonResult.data(fullRegionName);
        } catch (Exception e) {
            return CommonResult.error("获取完整地域信息失败：" + e.getMessage());
        }
    }

    /**
     * 获取完整的省市区树形结构数据
     */
    @GetMapping("/tree")
    @Operation(summary = "获取完整的省市区树形结构数据", description = "获取包含所有省份、城市、区县的完整树形结构数据")
    public CommonResult<List<RegionTreeVo>> getRegionTree() {
        try {
            List<RegionTreeVo> regionTree = regionService.getRegionTree();
            return CommonResult.data(regionTree);
        } catch (Exception e) {
            return CommonResult.error("获取省市区树形数据失败：" + e.getMessage());
        }
    }
}
